<template>
  <div class="app-container">
    <h2 style="text-align: center">字体和七步法</h2>

    <el-steps
      :active="1"
      process-status="wait"
      align-center
      style="margin-bottom: 40px"
    >
      <el-step title="添加字体" />
      <el-step title="添加七步" />
    </el-steps>
    <div>
      <el-form :inline="true" :model="dataForm" @keyup.enter.native="queryWordsList()">
        <el-form-item>
          <el-input
            v-model="dataForm.name"
            placeholder="字体名称"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="queryWordsList()">查询</el-button>
          <el-button type="primary" @click="addWord(0)">新增</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-table
      :data="wordList"
      stripe
      v-loading="wordLoading"
      style="width: 100%"
    >
      <el-table-column
        prop="id"
        header-align="center"
        align="center"
        label="字体id"
      ></el-table-column>
      <el-table-column
        prop="name"
        header-align="center"
        align="center"
        label="字体名称"
      ></el-table-column>
      <el-table-column
        prop="typefacePicture"
        header-align="center"
        align="center"
        label="字体图片"
      >
        <template slot-scope="scope">
          <el-image :src="scope.row.typefacePicture"
            ><div slot="error" class="image-slot">
              <i class="el-icon-picture-outline"></i></div></el-image></template
      ></el-table-column>
      <el-table-column header-align="center" align="center" label="操作">
        <template slot-scope="scope">
          <el-button type="text" @click="toXs(scope.row.id)">欣赏</el-button>
          <el-button type="text" @click="toRd(scope.row.id)">认读</el-button>
          <el-button type="text" @click="toGc(scope.row.id)">观察</el-button>
          <el-button type="text" @click="toBf(scope.row.id)">笔法</el-button>
          <el-button type="text" @click="toFx(scope.row.id)">分析</el-button>
          <el-button type="text" @click="toDb(scope.row.id)">对比</el-button>
          <el-button type="text" @click="toLx(scope.row.id)">练习</el-button>
          <el-button type="text" @click="toVideo(scope.row.id)">视频</el-button>
          <el-button style="" type="text" @click="addWord(1, scope.row.id)"
            >修改</el-button
          >
          <el-button type="text" @click="deleteById(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
    ></el-pagination>
    <el-dialog
      :visible.sync="dialogWordVisible"
      :title="type == 0 ? '添加字体信息' : '修改字体信息'"
    >
      <el-form
        :rules="dataRule"
        ref="wordForm"
        :model="word"
        label-width="120px"
      >
        <el-form-item label="字体名称" prop="name" style="width: 300px">
          <el-input v-model="word.name" />
        </el-form-item>
        <el-form-item label="字体图片">
          <el-upload
            class="avatar-uploader"
            :action="'http://121.41.167.195:8000/api/oss/upload'"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
          >
            <img
              v-if="word.typefacePicture"
              :src="word.typefacePicture"
              class="avatar"
            />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogWordVisible = false">取 消</el-button>
        <el-button
          :disabled="saveWordBtnDisabled"
          type="primary"
          @click="saveOrUpdate"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import Vue from "vue";
export default {
  data() {
    return {
      dataForm: { name: "" },
      buzhou: 0,
      wordLoading: false,
      saveBtnDisabled: false, // 保存按钮是否禁用
      word: {
        // 出版社对象
        name: "",
        typefacePicture: "",
        id: null,
      },
      wordList: [],
      type: 0, // 0:添加1:修改
      xzBtn: false,
      dialogWordVisible: false,
      saveWordBtnDisabled: false,
      dataRule: {
        name: [
          { required: true, message: "字体名称不能为空", trigger: "blur" },
        ],
      },
      pageIndex: 1, // 当前页
      pageSize: 10, // 每页数据显示几条
      totalPage: 0, // 总记录数
    };
  },

  activated() {
    // 有数据就要回显数据
    this.getWordList();
  },

  methods: {
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getWordList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getWordList();
    },
    queryWordsList(){
      this.pageIndex = 1;
      this.getWordList();
    },
    getWordList() {
      this.wordLoading = true;
      this.$http({
        url: this.$http.adornUrl(`/sf/cbtypeface/getWordList`),
        method: "get",
        params: this.$http.adornParams({
          page: this.pageIndex,
          limit: this.pageSize,
          name: this.dataForm.name,
        }),
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.wordList = data.wordPage.records;
          this.totalPage = data.wordPage.total;
          this.xzBtn = true;
        } else {
          this.wordList = [];
          this.totalPage = 0;
        }
        this.wordLoading = false;
      });
    },
    // 章节对话框中的保存或修改
    saveOrUpdate() {
      if (this.type == 0) {
        // 添加章节
        this.wordFormSubmit();
      } else {
        // 修改章节
        this.updateWord();
      }
    },
    //上传成功后的回调函数
    handleAvatarSuccess(response) {
      this.word.typefacePicture = response.url;
    },
    // 更新章节图片信息
    updateWord() {
      this.$refs["wordForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/sf/cbtypeface/update`),
            method: "post",
            data: this.$http.adornData(
              {
                id: this.word.id,
                name: this.word.name,
                typefacePicture: this.word.typefacePicture,
              },
              false
            ),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "修改成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.getWordList();
                  this.dialogWordVisible = false;
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
    // 打开对话框，添加、修改
    addWord(status, id) {
      // 重置表单 应该只是把表单有的清空而已
      this.$nextTick(() => {
        this.$refs.wordForm.resetFields();
      });
      if (status == 0) {
        // 添加
        this.type = 0;
        // 清空
        this.word.id = null;
      } else {
        // 修改
        this.type = 1;
        // 回显数据
        this.$http({
          url: this.$http.adornUrl("/sf/cbtypeface/info/" + id),
          method: "get",
        }).then(({ data }) => {
          this.word.id = data.cbTypeface.id;
          this.word.name = data.cbTypeface.name;
          this.word.typefacePicture = data.cbTypeface.typefacePicture;
        });
      }
      this.dialogWordVisible = true;
    },
    // 表单提交
    wordFormSubmit() {
      // 表单验证
      this.$refs["wordForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/sf/cbtypeface/save`),
            method: "post",
            data: this.$http.adornData(
              {
                name: this.word.name,
                typefacePicture: this.word.typefacePicture,
              },
              false
            ),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "添加成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  // 刷新
                  this.getWordList();
                  // 只能添加一次
                  this.xzBtn = true;
                  this.dialogWordVisible = false;
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
    // 通过章节id删除章节
    deleteById(id) {
      this.$confirm(`确定对[字体id=${id}]进行[删除]操作?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$http({
            url: this.$http.adornUrl("/sf/cbtypeface/delete"),
            method: "post",
            data: this.$http.adornData([id], false),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "删除成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.getWordList();
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        })
        .catch(() => {});
    },
    // 去欣赏
    toXs(id) {
      // 路由跳转
      this.$router.push({
        path: "/ruanbi/ruanbixsAdd/" + id,
      });
    },
    // 去认读
    toRd(id) {
      // 路由跳转
      this.$router.push({
        path: "/ruanbi/ruanbirdAdd/" + id,
      });
    },
    // 去观察
    toGc(id) {
      // 路由跳转
      this.$router.push({
        path: "/ruanbi/ruanbigcAdd/" + id,
      });
    },
    // 去分析
    toFx(id) {
      // 路由跳转
      this.$router.push({
        path: "/ruanbi/ruanbifxAdd/" + id,
      });
    },
    // 去笔法
    toBf(id) {
      // 路由跳转
      this.$router.push({
        path: "/ruanbi/ruanbibfAdd/" + id,
      });
    },
    // 去对比
    toDb(id) {
      // 路由跳转
      this.$router.push({
        path: "/ruanbi/ruanbidbAdd/" + id,
      });
    },
    // 去练习
    toLx(id) {
      // 路由跳转
      this.$router.push({
        path: "/ruanbi/ruanbistlxAdd/" + id,
      });
    },
    // 去视频
    toVideo(id) {
      // 路由跳转
      this.$router.push({
        path: "/ruanbi/wordVideo/" + id,
      });
    },
  },
};
</script>
<style scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.chanpterList {
  position: relative;
  list-style: none;
  margin: 0;
  padding: 0;
}
.chanpterList li {
  position: relative;
}
.chanpterList p {
  float: left;
  font-size: 20px;
  margin: 10px 0;
  padding: 10px;
  height: 70px;
  line-height: 50px;
  width: 100%;
  border: 1px solid #ddd;
}
.chanpterList .acts {
  float: right;
  font-size: 14px;
}

.videoList {
  padding-left: 50px;
}
.videoList p {
  float: left;
  font-size: 14px;
  margin: 10px 0;
  padding: 10px;
  height: 50px;
  line-height: 30px;
  width: 100%;
  border: 1px dotted #ddd;
}
</style>
